package main

// 连接mysql数据库, 生成数据库连接池，供后续使用
// 1. 导入mysql驱动包
// 2. 创建数据库连接池
// 3. 连接数据库
// 4. 执行sql语句
// 5. 关闭数据库连接

import (
	"database/sql"
	"fmt"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

/***
var id int
var created_at string
var updated_at string
var deleted_at string
var encode_code_str string
var unique_id int
var secret_key string
var batch_number string
var deadline string
***/

type Purifier struct {
	Id              int     `json:"id" db:"id"`
	Created_at      string  `json:"created_at" db:"created_at"`
	Updated_at      string  `json:"updated_at" db:"updated_at"`
	Deleted_at      *string `json:"deleted_at" db:"deleted_at"`
	Encode_code_str string  `json:"encode_code_str" db:"encode_code_str"`
	Unique_id       string  `json:"unique_id" db:"unique_id"`
	Secert_key      string  `json:"secret_key" db:"secret_key"`
	Batch_number    string  `json:"batch_number" db:"batch_number"`
	Deadline        string  `json:"deadline" db:"deadline"`
	Model           string  `json:"model" db:"model"`
}

// 滤网先绑定，才配对， 无需提示

type DecodePur struct {
	Encode_code_str string `json:"encode_code_str" db:"encode_code_str"`
	Unique_id       string `json:"unique_id" db:"unique_id"`
	Secert_key      string `json:"secret_key" db:"secret_key"`
	Batch_number    string `json:"batch_number" db:"batch_number"`
}

func GetCodemain() []DecodePur {
	// 2. 创建数据库连接池
	db, err := sql.Open("mysql", "dcroot:WEVkyOHPFjOyvbSGXU2fwVHXcoVs6Val@tcp(dc03.iotdreamcatcher.net.cn:5306)/dc_v2_factory?charset=utf8")
	if err != nil {
		panic(err)
	}
	// 3. 连接数据库
	defer db.Close()
	// 4. 执行sql语句
	// 4.1 查询sql
	sql := "select * from air_purifier"
	rows, err := db.Query(sql)
	if err != nil {
		panic(err)
	}
	// 4.2 获取结果
	// 4.2.1 获取结果中的列名
	var results []DecodePur
	var row Purifier
	for rows.Next() {
		err = rows.Scan(&row.Id, &row.Created_at, &row.Updated_at, &row.Deleted_at, &row.Encode_code_str, &row.Unique_id, &row.Secert_key, &row.Batch_number, &row.Deadline)
		if err != nil {
			panic(err)
		}
		//fmt.Println(row)
		results = append(results, DecodePur{
			Encode_code_str: row.Encode_code_str,
			Unique_id:       row.Unique_id,
			Secert_key:      row.Secert_key,
			Batch_number:    row.Batch_number,
		})
	}
	fmt.Println(results)
	return results
}

type PurifierDeviceBind struct {
	ID                int64     `db:"id" json:"id"`
	PurifierId        int64     `db:"purifier_id" json:"purifier_id"`               //  净水器id
	DeviceId          int64     `db:"device_id" json:"device_id"`                   //  设备id
	MildPollution     int64     `db:"mild_pollution" json:"mild_pollution"`         //  轻度污染
	ModeratePollution int64     `db:"moderate_pollution" json:"moderate_pollution"` //  中度污染
	RemainingTime     int64     `db:"remaining_time" json:"remaining_time"`         //  剩余时间
	BindTime          time.Time `db:"bind_time" json:"bind_time"`                   //  绑定时间
	UpdateTime        time.Time `db:"update_time" json:"update_time"`               //  解绑时间
}

func GetPurFierTime() (time.Time, int, int) {
	// 2. 创建数据库连接池
	db, err := sql.Open("mysql", "dcroot:WEVkyOHPFjOyvbSGXU2fwVHXcoVs6Val@tcp(dc03.iotdreamcatcher.net.cn:4306)/dc_v2_device?charset=utf8&parseTime=true")
	if err != nil {
		panic(err)
	}
	// 3. 连接数据库
	defer db.Close()
	// 4. 执行sql语句
	// 4.1 查询sql
	sql := "select * from purifier_device_bind limit 1"
	rows, err := db.Query(sql)
	if err != nil {
		panic(err)
	}
	// 4.2 获取结果
	// 4.2.1 获取结果中的列名
	var row PurifierDeviceBind
	for rows.Next() {
		err = rows.Scan(&row.ID, &row.PurifierId, &row.DeviceId, &row.MildPollution,
			&row.ModeratePollution, &row.RemainingTime, &row.BindTime, &row.UpdateTime)
		if err != nil {
			panic(err)
		}
	}
	return row.BindTime, int(row.MildPollution), int(row.ModeratePollution)
}
